<?php
/**
 * @Notes: 文件描述
 * @Interface getCondition
 * @Return mixed
 * @Author: bqs
 * @Time: 2021/4/19 15:28
 */

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\console\input\Option;
use think\Db;
use think\facade\Hook;
use think\facade\Log;
use QL\QueryList;
use QL\Ext\CurlMulti;

class QueryListSpider extends Command
{

    protected function configure()
    {
        $this->setName('query:list')
            ->setDescription('采集');
    }

    protected function execute(Input $input, Output $output)
    {
        ini_set('memory_limit', '512M');

        $output->writeln("=========date:" . date('Y-m-d H:i:s') . "===============");

        // 地址与目录映射
        $dirMap = [
            "假面骑士" => "https://zhuanlan.zhihu.com/p/376119915",
            "龙珠" => "https://zhuanlan.zhihu.com/p/340048917",
            "火影忍者" => ["https://zhuanlan.zhihu.com/p/352717188", "https://zhuanlan.zhihu.com/p/393213201", "https://zhuanlan.zhihu.com/p/358228745"],
            "海贼王" => ["https://zhuanlan.zhihu.com/p/357683518", "https://zhuanlan.zhihu.com/p/338160632"]
        ];

        // 采集地址
        $multiArr = [];
        $multiArr = array_reduce(array_values($dirMap), function ($res, $value) {
            $res = array_merge($res, (array)$value);
            return $res;
        }, []);

        // 采集映射
        $multiMap = [];
        foreach($dirMap as $key => $value) {
            if (!is_array($value)) {
                $multiMap[$value] = $key;
            } else {
                $temp = array_fill_keys($value, $key);
                $multiMap = array_merge($multiMap, $temp);
            }
        }

        // 开始使用多线程采集
        $ql = QueryList::use (CurlMulti::class);
        $ql->curlMulti($multiArr)
        ->success(function (QueryList $ql, CurlMulti $curl, $r) use ($multiMap) {

            $path = 'E:\2setsoft\1dev\phpstudy_pro\WWW\4test\tensorflowJs\js-ml-code\t7\动漫分类\train\\';
            $queryUrl = $r['info']['url'];
            $className = $multiMap[$queryUrl] ?? "";
            $targetDir = $path . $className;
            $path = $targetDir . '\\';

            $endFileIndex = 0;
            $existFileList = $this->scanFile($targetDir);
            if ($existFileList) {
                // 取出所有数字文件名最大值
                $endFileName = max($existFileList);
                $endFileIndex = explode(".", $endFileName)[0];
            }

            $data = $ql->find('.RichText')->find('noscript')->find('img')->attrs('src');

            foreach($data as $key => $value) {
                $index = $key + 1 + $endFileIndex;

                $filename = $index < 10 ? str_pad($index, 2, "0", STR_PAD_LEFT) : $index;
                $filend = pathinfo($value, PATHINFO_EXTENSION);
                $file = file_get_contents($value);
                file_put_contents($path . $filename . "." . $filend, $file);
            }
        })
        // 每个任务失败回调
        ->error(function ($errorInfo, CurlMulti $curl) {
            echo "Current url:{$errorInfo['info']['url']} \r\n";
            print_r($errorInfo['error']);
        })
        ->start([
            // 最大并发数
            'maxThread' => 10,
            // 错误重试次数
            'maxTry' => 5,
        ]);

        $output->writeln("============date:" . date("Y-m-d H:i:s") . "采集完成==============");
    }

    // 扫描目录下所有文件
    protected function scanFile($path) {
        $result = [];
        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                if (is_dir($path . '/' . $file)) {
                    $this->scanFile($path . '/' . $file);
                } else {
                    $result[] = basename($file);
                }
            }
        }
        return $result;
    }

}